tableJsAdditionalSubmitArgument = "";

function setAdditionalTableSubmitArgument(value) {
    tableJsAdditionalSubmitArgument = value;
}

function updateQueryStringParameter(uri, key, value) {
    var re = new RegExp("([?|&])" + key + "=.*?(&|#|$)", "i");
    if (uri.match(re)) {
        return uri.replace(re, '$1' + key + "=" + value + '$2');
    } else {
        var hash = '';
        var separator = uri.indexOf('?') !== -1 ? "&" : "?";
        if (uri.indexOf('#') !== -1) {
            hash = uri.replace(/.*#/, '#');
            uri = uri.replace(/#.*/, '');
        }
        return uri + separator + key + "=" + value + hash;
    }
}

function tableSubmit(tableId, event) {
    if (event.keyCode !== 13) {
        return;
    }
    var tableParent = $("#" + tableId).parent();
    var filterMessage = "";
    var first = true;
    $('#' + tableId + " input").each(function() {
        var value = $(this).val();
        if (!value || value.trim() === "") {
            return;
        }

        if (first === false) {
            filterMessage += "&";
        }

        var name = this.getAttribute('name');
        var component = name + "=" + encodeURIComponent(value);
        filterMessage += encodeURIComponent(component);
        first = false;
    });

    var filterAttribute = encodeURIComponent(filterMessage);
    var newUri = updateQueryStringParameter(window.location.href, 'tableFilter__' + tableId, filterAttribute);
    if(tableJsAdditionalSubmitArgument!==""){
        newUri = updateQueryStringParameter(newUri, 'additionalTblSbm', tableJsAdditionalSubmitArgument);
    }
    
    tableParent.load(newUri+" #"+tableId);
}